package com.yk.airline.component.analysisdata.impl;

import com.yk.airline.component.analysisdata.api.ICommonAnalysis;
import com.yk.airline.dao.analysisdata.api.IAnalysisDataDao;
import com.yk.airline.model.AnalysisDataModel;
import com.yk.airline.model.CaptureMarkModel;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.Instant;

/**
 * Project Name:airlinecrawler
 * File Name:XieChengDataAnalysis
 * Package Name:com.yk.airline.component.analysisdata.impl
 * Date:2018/2/1 15:55
 * Author:dengtianjia
 * Description:
 * Copyright (c) 2018, 重庆云凯科技有限公司 All Rights Reserved.
 */
@Service("xieChengDataAnalysis")
public class XieChengDataAnalysis extends CommonAnalysisImpl implements ICommonAnalysis {

    @Resource(name = "analysisDataDao")
    private IAnalysisDataDao analysisDataDao;

    @Override
    public void saveData(Element element, String date, CaptureMarkModel mark, String airLine) {
        AnalysisDataModel analysisDataModel = new AnalysisDataModel();
        if (!element.select("td.logo strong").isEmpty()) {
            String airCompany = element.select("td.logo strong").get(0).text();
            analysisDataModel.setAirCompany(airCompany);
        }
        if (!element.select("td.logo span").isEmpty()) {
            String flightNumber = element.select("td.logo span").get(0).text();
            analysisDataModel.setFlightNumber(flightNumber);
        }
        Double lowestPrice = 0.0;
        Elements basePriceElement = element.select(".J_base_price span");
        if (basePriceElement != null && !basePriceElement.isEmpty()) {
            lowestPrice = Double.valueOf(basePriceElement.first().ownText());
        }
        analysisDataModel.setLowestPrice(lowestPrice);
        if (!element.select(".J_pricePannel").isEmpty()) {
            Elements priceGroup = element.select(".J_pricePannel").get(0).select(".J_group");
            Double specialPrice = 0.0;
            Double preferredPrice = 0.0;
            StringBuilder preferredInfo = new StringBuilder();
            for (Element e : priceGroup) {
                String[] temps = e.text().split("\\s+");
                String temp = temps[0];
                if (temp.equals("特惠")) {
                    specialPrice = Double.valueOf(e.select(".flt_price span").get(0).ownText());
                }
                if (temp.equals("优选")) {
                    Element tempElement = e.select(".search_table_col").get(0);
                    preferredPrice = Double.valueOf(tempElement.select(".flt_price span").get(0).ownText());
                    if (!tempElement.select("div.conditions span[data-ubt=flightmarketingcode]").isEmpty()) {
                        preferredInfo = new StringBuilder(tempElement.select("div.conditions span[data-ubt=flightmarketingcode]").get(0).text());
                    }
                    Elements elements = tempElement.select("div.additional_operations_item");
                    for (Element current : elements) {
                        for (Element span : current.select("span.price-yellow ")) {
                            preferredInfo.append(span.text().replace("+",""));
                        }
                    }
                }
            }
            analysisDataModel.setSpecialPrice(specialPrice);
            analysisDataModel.setPreferredPrice(preferredPrice);
            analysisDataModel.setPreferredInfo(preferredInfo.toString());
        }
        Timestamp departureTime = null;
        Timestamp arriveTime = null;
        if (!element.select(".J_header_row .right .time").isEmpty()) {
            String departrueTimePart = element.select(".J_header_row .right .time").get(0).text();
            departureTime = Timestamp.valueOf(date + " " + departrueTimePart + ":00");
        }
        if (!element.select(".J_header_row .left .time").isEmpty()) {
            String arriveTimePart = element.select(".J_header_row .left .time").get(0).text();
            arriveTime = Timestamp.valueOf(date + " " + arriveTimePart + ":00");
        }
        analysisDataModel.setDepartureTime(departureTime);
        analysisDataModel.setArriveTime(arriveTime);
        analysisDataModel.setCreateTime(Timestamp.from(Instant.now()));
        analysisDataModel.setUpdateTime(Timestamp.from(Instant.now()));
        analysisDataModel.setWebsite("携程");
        analysisDataModel.setCaptureMarkByCaptureTime(mark);
        analysisDataModel.setAirLine(airLine);
        analysisDataDao.save(analysisDataModel);
    }
}
